package com.bw.lib_log.logger

import com.bw.lib_log.LoggerLevel
import com.bw.lib_log.LoggerStrategy

class Logger constructor( // 2.主构造
    private val TAG: String,
    private val level: LoggerLevel,
    private val strategy: LoggerStrategy,
    private val isDebug: Boolean,
    private val saveUrl: String) {
    //2.次构造继承主构造
    constructor(builder: Builder):this(
        builder.TAG,
        builder.level,
        builder.strategy,
        builder.isDebug,
        builder.saveUrl
    )
    /**
     * 3.策略者模式：根据不同策略创建不同的logger对象
     */
    private var logger:ILogger//日志logger
    init {
        when(strategy){
            LoggerStrategy.LOG_CAT -> logger = LogCatLogger()
            LoggerStrategy.FILE -> logger = FileLogger()
            LoggerStrategy.NET -> logger = NetLogger()
        }
        logger.setDebug(isDebug)
        logger.setTag(TAG)
        logger.setSavaUrl(saveUrl)
        logger.setLevel(level)
    }
    //4 打印函数
    /**
     * 输出Debug log
     */
    fun d(Tag:String,log:String){
        logger.d(Tag,log)
    }

    /**
     * 输出Warn log
     */
    fun w(Tag:String,log:String){
        logger.w(Tag,log)
    }

    /**
     * 输出Verbose log
     */
    fun v(Tag:String,log:String){
        logger.v(Tag,log)
    }

    /**
     * 输出Error log
     */
    fun e(Tag:String,log:String){
        logger.e(Tag,log)
    }
    /**
     * 输出Info log
     */
    fun i(Tag:String,log:String){
        logger.i(Tag,log)
    }



    /**
     * 1.构建者设计模式：多个参数的按需设置参数，不需要全部设置
     */
    class Builder{
        var TAG:String = "" //
        var level: LoggerLevel = LoggerLevel.DEBUG //默认等级
        var strategy: LoggerStrategy = LoggerStrategy.LOG_CAT //默认策略
        var isDebug:Boolean = true
        var saveUrl:String = ""

        fun setTAG(TAG:String):Builder{
            this.TAG = TAG
            return this
        }
        fun setLevel(level: LoggerLevel):Builder{
            this.level = level
            return this
        }
        fun setStrategy(strategy: LoggerStrategy):Builder{
            this.strategy = strategy
            return this
        }
        fun setDebug(isDebug:Boolean):Builder{
            this.isDebug = isDebug
            return this
        }
        fun setSaveUrl(saveUrl:String):Builder{
            this.saveUrl = saveUrl
            return this
        }
        fun build():Logger{
            return Logger(this)
        }

    }

}